import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# online_data = pd.read_csv("input/ccf_online_stage1_train.csv")
offline_data = pd.read_csv("input/ccf_offline_stage1_train.csv")

off_not_null = offline_data[offline_data['Coupon_id'].notnull()]
off_null = offline_data[offline_data['Coupon_id'].isnull()]

# 用户人数
user_count = offline_data['User_id'].unique()
len(user_count)
# 商户人数
merchant_count = offline_data['Merchant_id'].unique()
len(merchant_count)

# 领取优惠券与未领取人数对比
offline_data['get'] = offline_data['Coupon_id'].isnull()
offline_get = offline_data.groupby("get").count().reset_index()
fig,ax = plt.subplots()
p = ax.bar(range(len(offline_get)),offline_get['User_id'],tick_label=offline_get['get'])
plt.show()

# 优惠券领取率
coupon_get_rate = len(offline_data[offline_data['Coupon_id'].notnull() & offline_data['Date'].notnull()])/len(offline_data)
# 优惠券使用率
coupon_user_rate = len(offline_data[offline_data['Coupon_id'].notnull() & offline_data['Date'].notnull()])/len(offline_data[offline_data['Coupon_id'].notnull()])

# 领取优惠券的人
coupon_get = offline_data[offline_data['Coupon_id'].notnull()]
#使用优惠券的人
coupon_use = offline_data[offline_data['Coupon_id'].notnull() & offline_data['Date'].notnull()]

# 门店优惠券领取率TOP20
coupon_get_merchants_top200 = coupon_get.groupby("Merchant_id").count().sort_values(by='User_id',ascending=False).reset_index()[:200]
fig,ax = plt.subplots()
plt.bar(range(len(coupon_get_merchants_top200)),coupon_get_merchants_top200['User_id'])
plt.show()
# 门店优惠券使用率TOP20
coupon_use_merchants_top200 = coupon_use.groupby("Merchant_id").count().sort_values(by="User_id",ascending=False).reset_index()[:200]
fig,ax = plt.subplots()
plt.bar(range(len(coupon_use_merchants_top200)),coupon_use_merchants_top200['User_id'])
plt.show()
# 领取未使用的人计数
coupon_not_use_merchants_top200 = coupon_get_merchants_top200-coupon_use_merchants_top200
fig,ax = plt.subplots()
plt.bar(range(len(coupon_not_use_merchants_top200)),coupon_not_use_merchants_top200['User_id'])
plt.bar(range(len(coupon_use_merchants_top200)),coupon_use_merchants_top200['User_id'],bottom=coupon_not_use_merchants_top200['User_id'])
plt.show()


# 针对店铺：分析优惠券使用人数与未使用人数
merchants_coupon = coupon_get.groupby("Merchant_id")
merchants_coupon = merchants_coupon.count().sort_values("Date",ascending=False)
most_use_coupon = merchants_coupon[['User_id','Date']].rename(columns={"User_id":"Coupon_get","Date":"Coupon_use"})[:100]
most_use_coupon['Coupon_not_use'] = most_use_coupon['Coupon_get']-most_use_coupon['Coupon_use']

# 优惠券领取后的使用率
most_use_coupon['use_rate'] = most_use_coupon['Coupon_use']/most_use_coupon['Coupon_get']
most_use_coupon = most_use_coupon.sort_values("use_rate",ascending=False)
plt.bar(range(len(most_use_coupon)),most_use_coupon['Coupon_use'])
plt.bar(range(len(most_use_coupon)),most_use_coupon['Coupon_not_use'],bottom=most_use_coupon['Coupon_use'])
plt.show()


# 判断距离远近与个数的关系
off_distance = offline_data.groupby("Distance").count().reset_index()[['Distance','User_id']]

fig, ax = plt.subplots()

bar_width = 0.35
p = ax.bar(range(len(off_distance)),off_distance['User_id'],tick_label=off_distance['Distance'])

# 获取领取并使用了消费券的用户的使用周期的趋势
# 领取并使用了优惠券的用户
coupon_used = off_not_null[off_not_null['Date'].notnull()&off_not_null['Date_received'].notnull()]

coupon_used['period'] = coupon_used['Date']-coupon_used['Date_received']

coupon_used_period_count = coupon_used.groupby("period").count().reset_index()[['period','User_id']].rename(columns={"period":"period","User_id":"count"})
coupon_used_period_count = coupon_used_period_count.sort_values(by="period",axis=0,ascending=False)[:70]
p = ax.bar(range(len(coupon_used_period_count)),coupon_used_period_count['count'],tick_label=coupon_used_period_count['period'])

# 问题：为什么会有两个峰值？

# 对优惠券的额度对用户使用情况的影响进行分析
coupon_used = off_not_null[off_not_null['Date'].notnull()&off_not_null['Date_received'].notnull()]

coupon_used_discount_rate = coupon_used.groupby("Discount_rate").count().reset_index()[['Discount_rate','User_id']].rename(columns={"Discount_rate":"Discount_rate","User_id":"count"})
# 使用了优惠券的用户对优惠券种类计数

# 总优惠券领取数量
coupon_total_discount_rate = off_not_null.groupby("Discount_rate").count().reset_index()[['Discount_rate','User_id']].rename(columns={"Discount_rate":"Discount_rate","User_id":"count"})
coupon_total_discount_rate = coupon_total_discount_rate.sort_values(by='count',axis=0,ascending=False)
# 排序
coupon_total_discount_rate = coupon_total_discount_rate.sort_values(by='Discount_rate',axis=0,ascending=False)
coupon_used_discount_rate = coupon_used_discount_rate.sort_values(by='Discount_rate',axis=0,ascending=False)
p = ax.bar(range(len(coupon_used_discount_rate)),coupon_used_discount_rate['count'],tick_label=coupon_used_discount_rate['Discount_rate'])

# 测试iterative mode
#-------------------------------------------#
# 各种优惠券领取量与使用量对比
plt.ioff()
plt.plot(coupon_total_discount_rate['count'])
plt.plot(coupon_used_discount_rate['count'])
plt.show()

# 归一化后份额对比

coupon_total_sum = coupon_total_discount_rate['count'].sum()
coupon_used_sum = coupon_used_discount_rate['count'].sum()
coupon_total_discount_rate['count'] = coupon_total_discount_rate['count']/coupon_total_sum
coupon_used_discount_rate['count'] = coupon_used_discount_rate['count']/coupon_used_sum
# 去除占比小于0.05的点
main_total_points = coupon_total_discount_rate[coupon_total_discount_rate['count']>0.05]
main_used_points = coupon_used_discount_rate[coupon_used_discount_rate['count']>0.005]
# 绘图
plt.ioff()
plt.plot(coupon_total_discount_rate['count'],marker='*',label="total")
plt.plot(coupon_used_discount_rate['count'],marker='o',label='used')
plt.legend()
# 设置x坐标轴内容
length = coupon_total_discount_rate['count'].count()
name = coupon_used_discount_rate['Discount_rate']
plt.xticks(range(length),name,rotation=0)
plt.show()

# 用户消费频次对比
off_frequency_user = offline_data.groupby("User_id").count().reset_index("User_id")
off_frequency_total = off_frequency_user.groupby("Merchant_id").count().reset_index()['User_id'].rename(columns={"Merchant_id":"frequency","User_id":"times"})[['frequency','times']]
fig, ax = plt.subplots()
p = ax.bar(range(len(off_frequency_total)),off_frequency_total['times'],tick_label=off_frequency_total['frequency'])

#------------------------------#
#以上为对数据总体结构的分析，下部分为对单个用户的分析

#单个用户消费量


# 线下领取消费券的次数


#------------------------------------------------------------------------------------------#

# 领取优惠券用户比例
offline_data['get_coupon'] = offline_data['Coupon_id'].isnull()
get_coupon_or_not = offline_data.groupby("get_coupon").count().reset_index()
get_coupon_or_not
fig,ax = plt.subplots()
p = ax.bar(range(len(get_coupon_or_not)),get_coupon_or_not['User_id'],tick_label=get_coupon_or_not['get_coupon'])
plt.show()

# 领取并使用了优惠券用户的比例
offline_data['use_coupon'] = offline_data['get_coupon']==True & offline_data['Date'].notnull()
use_coupon_in_get = offline_data.groupby('use_coupon').count().reset_index()
fig , ax = plt.subplots()
p = ax.bar(range(len(use_coupon_in_get)),use_coupon_in_get['User_id'],tick_label=use_coupon_in_get['use_coupon'])
plt.show()

# 领取优惠券与使用优惠券的平均间隔


coupon_used_period_count = offline_data[offline_data['Date_received'].notnull() & offline_data['Date'].notnull()]
coupon_used_period_count['user_period'] = offline_data['Date']-offline_data['Date_received']
coupon_used_period_count = coupon_used_period_count.groupby("user_period").count().reset_index()[['user_period','User_id']].rename(columns={"user_period":"period","User_id":"count"})
coupon_used_period_count = coupon_used_period_count.sort_values(by="period",axis=0,ascending=True)[:70]
fig , ax = plt.subplots()
p = ax.bar(range(len(coupon_used_period_count)),coupon_used_period_count['count'],tick_label=coupon_used_period_count['period'])
plt.show()

#------------------------------------------------------------------------------------------#
# 单个用户特征分析

users = offline_data.groupby("User_id")
# 将整型转日期
#offline_data = pd.read_csv("datalab/4901/ccf_offline_stage1_train.csv")
offline_data['Date']=pd.to_datetime(offline_data['Date'],format='%Y%m%d',errors='ignore')
offline_data['Date_received']=pd.to_datetime(offline_data['Date_received'],format='%Y%m%d',errors='ignore')
offline_data['period'] = offline_data['Date']-offline_data['Date_received']
# 平均使用间隔
users_mean_use = offline_data.groupby("period").count().reset_index()
users_mean_use.head()
# 单个用户消费的次数
user_customer = offline_data.groupby('User_id').count().reset_index()
# 单个用户领取优惠券的次数
user_get_coupon = offline_data[offline_data['Date_received'].notnull()].count().reset_index()
# 单个用户使用优惠券的次数
user_use_coupon = offline_data[offline_data['Date_received'].notnull() & offline_data['Date'].notnull()].count().reset_index()

# 单个用户领取优惠券占总消费的比例
user_customer['get_ratio'] = user_customer['Merchant_id']/user_get_coupon['Merchant_id']
# 单个用户使用消费券占总优惠券的比例
user_customer['use_ratio'] = user_customer['Merchant_id']/user_use_coupon['Merchant_id']
# use_mean_period = (users['Date'].mean()-users['Date_received'].mean()).reset_index()
# use_mean_period = use_mean_period.rename(columns={0:"mean_period"})
# use_mean_period['mean_period'] = use_mean_period['mean_period']+0.5
# use_mean_period['mean_period'] = use_mean_period['mean_period'].apply(np.round)
# # 去除空选项
# use_mean_period = use_mean_period[use_mean_period['mean_period'].notnull()]
# # 单个用户平均使用优惠券间隔
# mean_period = use_mean_period.groupby('mean_period').count().reset_index()
# mean_period


